package com.yangwp.doutu.service.impl;

import com.yangwp.doutu.dao.LikeImgDao;
import com.yangwp.doutu.model.LikeImg;
import com.yangwp.doutu.service.LikeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private LikeImgDao likeImgDao;

    @Override
    public Boolean add(String userId, String img){
         if(StringUtils.isBlank(userId) || StringUtils.isBlank(img)){
             return Boolean.FALSE;
         }

         LikeImg likeImg = new LikeImg();
         likeImg.setUserId(userId);
         likeImg.setImg(img);
         likeImg.setCreateTime(new Date());
         try {
             return likeImgDao.insert(likeImg) > 0;
         }catch (Exception e){
             return Boolean.FALSE;
         }
    }

    @Override
    public Boolean isLikeExists(String userId, String img){
        Map<String, Object> params = new HashMap<>();
        params.put("img", img);
        params.put("userId", userId);
        try {
            List<LikeImg> likeImgList = likeImgDao.getLikeImgList(params);
            if(likeImgList == null || likeImgList.size() == 0){
                return Boolean.FALSE;
            }
            return Boolean.TRUE;
        }catch (Exception e){
            return Boolean.FALSE;
        }
    }

    @Override
    public Integer getLikeNum(String image){
        if(StringUtils.length(image) != 32){
            return 0;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("img", image);
        try {
            return likeImgDao.countLikeImg(param);
        }catch (Exception e){
            System.out.println(e);
            return 0;
        }
    }
}
